package cn.bdqn.easyexcel_web.listener;

import java.util.ArrayList;
import java.util.List;


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

import cn.bdqn.easyexcel_web.pojo.User;
import cn.bdqn.easyexcel_web.utils.UserServiceUtil;

public class ExcelListener extends AnalysisEventListener<User>{
	private List<User> list = new ArrayList<>();
	public ExcelListener() {}
	@Override
	public void invoke(User user, AnalysisContext context) {
		list.add(user);
	}

	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {
		List<User>users=transfor(list);
		for (int i = 0; i < users.size(); i++) {
			UserServiceUtil.getInstance().getUserService().add(users.get(i));
			System.out.println("存储第"+(i+1)+"行数据");
		}
	}
	public List<User> transfor(List<User>userInfo){
		List<User>users=new ArrayList<>();
		User u=null;
		int error=0;
		for (int i = 0; i < userInfo.size(); i++) {
			u=userInfo.get(i);
			if(u!=null) {
				if(u.getSexName().equals("男")) {
					u.setSex(1);
				}else if(u.getSexName().equals("女")) {
					u.setSex(0);
				}else {
					error++;
					continue;
				}
				users.add(u);
			}
		}
		System.out.println(error+"条数据导入失败");
		return users;
	}

}
